<?php


namespace app\admin\model;


class ComponentPurchased extends Base
{
    /**
     * $projects_order_id 项目订单
     * $purchased = [
     *  [id=>1, 外购件id
     *  num=>3] 外购件数量
     * ];
     * is_update 区分 编辑信息与修改信息提示信息不同
     * Desc: 通知仓储部门 订单已生成信息内容
     * User: 苏鹏
     * Date: 2023/10/23
     * Time: 11:19
     * @return void
     */
    public function getToWarehouseNoticeContent(int $projects_order_id, array $purchased, int $is_update = 0)
    {
        $order_result = ProjectsOrder::where('id', $projects_order_id)->where('status', 1)->field('id, name')->find();
        if(!empty($order_result))
        {
            $order_result = $order_result->toArray();
            if($is_update == 0){
                $content = '新增：'.'id：'.$order_result['id'].' 名称：'.$order_result['name'].' 的订单填写了外购件需求数量<br/>';
            }else{
                $content = '修改：'.'id：'.$order_result['id'].' 名称：'.$order_result['name'].' 的订单修改了外购件需求数量<br/>';
            }
            //查询外购件详细信息
            $new_purchased = [];
            foreach($purchased as $key=>$val)
            {
                $new_purchased[$val['id']] = $val['num'];
            }
            $ids = array_column($purchased, 'id');
            $ids = implode(',', $ids);
            $result = $this->where('id', 'in', $ids)->field('id,name,number')->select()->toArray();
            $now_time = date('Y-m-d H:i:s',strtotime('now'));
            foreach($result as $k=>$v)
            {
                $content .= 'id：'.$v['id'].' 部件名称：'.$v['name'].$now_time.'仓库数量：'.$v['number'].'订单需求数量：'.$new_purchased[$v['id']].'<br/>';
            }
            return $content;
        }else{
            return '';
        }
    }

    /**
     * Desc: 通知仓储部门 订单被删除
     * User: 苏鹏
     * Date: 2023/10/25
     * Time: 21:31
     * @return void
     */
    public function getToWarehouseDelPurchNoticeContent($id)
    {
        $order_result = ProjectsOrder::where('id', $id)->field('id, name')->find();
        if(!empty($order_result))
        {
            $order_result = $order_result->toArray();
            $content = '取消：'.'id：'.$order_result['id'].' 名称：'.$order_result['name'].' 的订单填写了外购件需求数量<br/>';
            //查询关联外购件详细信息
            $re_pruch_result = CompReCompPurch::where('component_id', '=', $id)->field('comp_purch_id,number')->select()->toArray();
            if(!empty($pruch_result))
            {
                $re_pruch_ids = [];
                $re_pruch_array = [];
                foreach($re_pruch_result as $k=>$v)
                {
                    $re_pruch_ids[] = $v['comp_purch_id'];
                    $re_pruch_array[$v['comp_purch_id']] = $v['number'];
                }
                $result = $this->where('id', 'in', $re_pruch_ids)->field('id,name,number')->select()->toArray();
                $now_time = date('Y-m-d H:i:s',strtotime('now'));
                foreach($result as $k=>$v)
                {
                    $content .= 'id：'.$v['id'].' 部件名称：'.$v['name'].$now_time.'仓库数量：'.$v['number'].'订单取消数量：'.$re_pruch_array[$v['id']].'<br/>';
                }
                return $content;
            }

        }
        return '';
    }
}